package com.huawei.appgallery.search.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.text.style.ReplacementSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.hwid.core.constants.HwAccountConstants;
import huawei.widget.HwTextView;
import java.lang.ref.WeakReference;
import o.cuv.b;
import o.cuv.c;
import o.cuv.d;
import o.dof;
import o.evf;

public class TagTextView extends HwTextView {
    private String ˋ;
    private TextView ॱ;

    static class e extends ReplacementSpan {
        private int ˋ;
        private int ˎ;
        private WeakReference<TextView> ॱ;

        public e(TextView textView) {
            this.ॱ = new WeakReference(textView);
            if (textView != null) {
                textView.measure(0, 0);
                this.ˎ = textView.getMeasuredWidth();
                this.ˋ = textView.getMeasuredHeight();
            }
        }

        public int getSize(@NonNull Paint paint, CharSequence charSequence, int i, int i2, @Nullable FontMetricsInt fontMetricsInt) {
            if (fontMetricsInt != null) {
                FontMetricsInt fontMetricsInt2 = paint.getFontMetricsInt();
                fontMetricsInt.ascent = fontMetricsInt2.top;
                fontMetricsInt.top = fontMetricsInt2.top;
                fontMetricsInt.descent = fontMetricsInt2.descent;
                fontMetricsInt.bottom = fontMetricsInt2.bottom;
            }
            return this.ˎ;
        }

        public void draw(@NonNull Canvas canvas, CharSequence charSequence, int i, int i2, float f, int i3, int i4, int i5, @NonNull Paint paint) {
            TextView textView = (TextView) this.ॱ.get();
            if (textView != null) {
                FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
                int i6 = ((fontMetricsInt.ascent + ((fontMetricsInt.descent + i4) + i4)) / 2) - (this.ˋ / 2);
                canvas.save();
                canvas.translate(f, (float) i6);
                textView.layout(0, 0, this.ˎ, this.ˋ);
                textView.draw(canvas);
                canvas.restore();
            }
        }
    }

    public TagTextView(Context context) {
        this(context, null);
    }

    public TagTextView(Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public TagTextView(Context context, @Nullable AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.ˋ = "TAG";
    }

    private void ॱ() {
        this.ॱ = (TextView) LayoutInflater.from(getContext()).inflate(o.cuv.e.ˊˋ, null).findViewById(b.ˉॱ);
        evf.ˎ(this.ॱ, c.ˋ);
        evf.ˋ(this.ॱ, d.ˎ);
    }

    public void setData(String str, String str2, int i) {
        if (TextUtils.isEmpty(str2)) {
            if (dof.ॱ(str)) {
                str = "";
            }
            setText(str);
            return;
        }
        if (this.ॱ == null) {
            ॱ();
        }
        this.ॱ.setText(str2);
        CharSequence spannableStringBuilder = new SpannableStringBuilder();
        spannableStringBuilder.append(ˋ(new e(this.ॱ)));
        Drawable colorDrawable = new ColorDrawable(0);
        colorDrawable.setBounds(0, 0, i, 0);
        spannableStringBuilder.append(ˋ(new ImageSpan(colorDrawable)));
        spannableStringBuilder.append(str);
        setText(spannableStringBuilder);
        setContentDescription(str2 + HwAccountConstants.BLANK + str);
    }

    private SpannableString ˋ(ReplacementSpan replacementSpan) {
        SpannableString spannableString = new SpannableString(this.ˋ);
        spannableString.setSpan(replacementSpan, 0, this.ˋ.length(), 17);
        return spannableString;
    }
}
